package com.genghis.ptas.note.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.note.dao.NoteDao;
import com.genghis.ptas.note.dao.PublicityNoteDao;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.note.entity.PublicityNote;
import com.genghis.ptas.note.service.PublicityNoteService;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.DateUtil;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("publicityNoteService")
public class PublicityNoteServiceImpl implements PublicityNoteService {

    @Autowired
    private PublicityNoteDao publicityNoteDao;

    @Autowired
    private NoteDao noteDao;

    @Autowired
    private DictService dictService;

    @Override
    public int addPublicityNote(PublicityNote publicityNote) {
        publicityNote.setState(DictConstant.AUDITING);
        publicityNote.setPublicityDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        return publicityNoteDao.addPublicityNote(publicityNote);
    }

    @Override
    public Page<PublicityNote> findPublicityNotePageList(PageParameter pageParameter, Note note) {
        Page<PublicityNote> publicityNotePage = publicityNoteDao.findPublicityNotePageList(pageParameter, note);
        for (PublicityNote publicityNote : publicityNotePage.getResultList()) {
            publicityNote.setNote(noteDao.findNoteById(publicityNote.getNoteId()));
            publicityNote.setShowState(dictService.changeDictCodeToValue(publicityNote.getState(), DictConstant.AUDIT_STATE));
        }
        return publicityNotePage;
    }

    @Override
    public void auditNotes(String ids, int state) {
        publicityNoteDao.auditNotes(ids, state);
    }

    @Override
    public PublicityNote findNoteStateById(int id) {
        PublicityNote publicityNote = publicityNoteDao.findNoteStateById(id);
        if (null != publicityNote) {
            if (publicityNote.getState() == DictConstant.DATE_INT) {
                publicityNote.setShowState("未审核");
            } else {
                publicityNote.setShowState(dictService.changeDictCodeToValue(publicityNote.getState(), DictConstant.AUDIT_STATE));
            }
        }
        return publicityNote;
    }

    @Override
    public void delPublicityNote(String id) {
        publicityNoteDao.delPublicityNote(id);
    }
}
